<template>
  <div class="register-container">
    <div class="platform-info">
      <h1 class="platform-title">社区养老综合服务平台</h1>
      <div class="platform-features">
        <div class="feature-item">
          <font-awesome-icon icon="heart" class="feature-icon" />
          <h3>健康管理</h3>
          <p>专业的健康监测和管理服务</p>
        </div>
        <div class="feature-item">
          <font-awesome-icon icon="users" class="feature-icon" />
          <h3>社交活动</h3>
          <p>丰富多彩的社区活动</p>
        </div>
        <div class="feature-item">
          <font-awesome-icon icon="hands-helping" class="feature-icon" />
          <h3>志愿服务</h3>
          <p>互助共享的时间银行</p>
        </div>
      </div>
    </div>
    <div class="register-box">
      <div class="title-container">
        <h2>用户注册</h2>
      </div>
      <div class="form-container">
        <div class="input-group">
          <font-awesome-icon icon="user" class="input-icon" />
          <input 
            v-model="username" 
            type="text" 
            placeholder="请输入账号"
            class="form-input" 
          />
        </div>
        <div class="input-group">
          <font-awesome-icon icon="lock" class="input-icon" />
          <input 
            v-model="password" 
            :type="showPassword ? 'text' : 'password'" 
            placeholder="请输入密码"
            class="form-input" 
          />
          <button 
            @click="togglePassword" 
            type="button"
            class="toggle-password"
            :title="showPassword ? '隐藏密码' : '显示密码'"
          >
            <font-awesome-icon :icon="showPassword ? 'eye-slash' : 'eye'" />
          </button>
        </div>
        <div class="input-group">
          <font-awesome-icon icon="lock" class="input-icon" />
          <input 
            v-model="confirmPassword" 
            :type="showConfirmPassword ? 'text' : 'password'" 
            placeholder="请确认密码"
            class="form-input" 
          />
          <button 
            @click="toggleConfirmPassword" 
            type="button"
            class="toggle-password"
            :title="showConfirmPassword ? '隐藏密码' : '显示密码'"
          >
            <font-awesome-icon :icon="showConfirmPassword ? 'eye-slash' : 'eye'" />
          </button>
        </div>
        <button @click="handleRegister" type="button" class="submit-btn register-btn">注册</button>
        <button @click="goToLogin" type="button" class="submit-btn login-btn">返回登录</button>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: '',
      confirmPassword: '',
      showPassword: false,
      showConfirmPassword: false
    }
  },
  methods: {
    handleRegister() {
      if (!this.username || !this.password || !this.confirmPassword) {
        alert('请填写所有必填项');
        return;
      }
      if (this.password !== this.confirmPassword) {
        alert('两次输入的密码不一致');
        return;
      }

      // 获取已存在的用户列表
      let users = JSON.parse(localStorage.getItem('users') || '[]');
      
      // 检查用户名是否已存在
      if (users.some(user => user.username === this.username)) {
        alert('该用户名已被注册');
        return;
      }

      // 添加新用户
      users.push({
        username: this.username,
        password: this.password
      });

      // 保存更新后的用户列表
      localStorage.setItem('users', JSON.stringify(users));
      
      alert('注册成功，请登录');
      this.$router.push('/login');
    },
    goToLogin() {
      this.$router.push('/login');
    },
    togglePassword() {
      this.showPassword = !this.showPassword;
    },
    toggleConfirmPassword() {
      this.showConfirmPassword = !this.showConfirmPassword;
    }
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #e0f7fa 0%, #b2ebf2 100%);
  padding: 40px;
  gap: 60px;
}

.platform-info {
  flex: 1;
  max-width: 600px;
  color: #2c3e50;
  padding: 40px;
}

.platform-title {
  font-size: 36px;
  font-weight: 600;
  margin-bottom: 40px;
  text-align: center;
  color: #2c3e50;
  text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.1);
}

.platform-features {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 30px;
  margin-top: 40px;
}

.feature-item {
  background: rgba(255, 255, 255, 0.9);
  padding: 30px;
  border-radius: 16px;
  text-align: center;
  transition: transform 0.3s ease;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
}

.feature-item:hover {
  transform: translateY(-5px);
}

.feature-icon {
  font-size: 36px;
  color: #3498db;
  margin-bottom: 20px;
}

.feature-item h3 {
  font-size: 20px;
  color: #2c3e50;
  margin-bottom: 10px;
}

.feature-item p {
  color: #7f8c8d;
  font-size: 14px;
}

.register-box {
  background: white;
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 450px;
  padding: 50px;
}

.title-container {
  text-align: center;
  margin-bottom: 40px;
}

h2 {
  color: #2c3e50;
  font-size: 24px;
  font-weight: 600;
}

.form-container {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.input-group {
  position: relative;
}

.input-icon {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: #95a5a6;
  font-size: 18px;
}

.form-input {
  width: 100%;
  padding: 14px 15px 14px 40px;
  border: 2px solid #e0e0e0;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
  background: #f8f9fa;
}

.form-input:focus {
  border-color: #3498db;
  outline: none;
  box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
}

.toggle-password {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #3498db;
  cursor: pointer;
  padding: 4px 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.toggle-password:hover {
  color: #2980b9;
}

.submit-btn {
  width: 100%;
  padding: 14px;
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.3s ease;
}

.register-btn {
  background: #3498db;
  color: white;
}

.register-btn:hover {
  background: #2980b9;
}

.login-btn {
  background: #fff;
  color: #3498db;
  border: 2px solid #3498db;
}

.login-btn:hover {
  background: #f8f9fa;
}

@media (max-width: 1024px) {
  .register-container {
    flex-direction: column;
    padding: 20px;
  }

  .platform-info {
    padding: 20px;
  }

  .platform-title {
    font-size: 28px;
  }

  .register-box {
    padding: 30px;
  }
}
</style>